/*
 * @Author: jqm123 2289662078@qq.com
 * @Date: 2025-08-08 17:19:02
 * @LastEditors: jqm123 2289662078@qq.com
 * @LastEditTime: 2025-08-15 10:42:43
 * @FilePath: /anxinxiu_v3/vite.config.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import { loadEnv } from "vite";
import { resolve } from "path";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { VantResolver } from "@vant/auto-import-resolver";
import autoprefixer from "autoprefixer";
import mobileForever from "postcss-mobile-forever";

import postCssPxToRem from "postcss-pxtorem";

// https://vite.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd());
  // 需要转换的 fixed 选择器列表
  const rootContainingBlockSelectorList = [
    ".van-tabbar",
    ".van-popup",
    ".van-popup--bottom",
    ".van-popup--top",
    ".van-popup--left",
    ".van-popup--right",
    // 在这里添加你的选择器
  ];

  return {
    base: "/",
    plugins: [
      vue(),
      AutoImport({
        resolvers: [VantResolver()],
      }),
      Components({
        resolvers: [VantResolver()],
        dts: "src/components.d.ts",
      }),
    ],
    css: {
      // postcss: {
      //   plugins: [
      //     postCssPxToRem({
      //       // 自适应，px>rem转换
      //       rootValue: 126,
      //       propList: ["*"],
      //     }),
      //   ],
      // },
      plugins: [
        autoprefixer(),
        mobileForever({
          viewportWidth: 750, // 设计稿宽度（基于 iPhone 6/7/8）
          appSelector: "#app", // 根容器选择器
          maxDisplayWidth: 600, // 桌面端最大显示宽度
          border: true, // 显示边界线用于调试
          rootContainingBlockSelectorList,
        }),
      ],
      loaderOptions: {
        less: {
          lessOptions: {
            modifyVars: {
              "@tabbar-item-font-size": "30px",
              "grid-item-content-background-color": "transparent",
              "field-placeholder-text-color": "#000",
            },
            javascriptEnabled: true,
          },
        },
      },
    },
    server: {
      host: true,
      port: 8081,
      open: true,
      hmr: true,
      proxy: {
        "/api": {
          target: env.VITE_API_BASE_URL,
          ws: false,
          changeOrigin: true,
        },
      },
    },
    resolve: {
      alias: {
        "@": resolve(__dirname, "./src"),
      },
    },
    build: {
      outDir: "dist",
      assetsDir: "static",
      sourcemap: mode !== "production",
      minify: mode === "production",
      assetsInlineLimit: 2048,
      terserOptions: {
        compress: {
          drop_console: true,
          drop_debugger: true,
        },
      },
      rollupOptions: {
        output: {
          assetFileNames: "assets/[hash][extname]",
          chunkFileNames: "js/[hash].js",
        },
      },
    },
  };
});
